var datatable;// 定义一个全局的dataTable
var attachChar;
var epsAttachChar;
var areaAttachChar;
var d;
//第二版变更开始**********
//查询参数
var param = new Object({
	init:function(){
		this.timeSize = ''
		this.beginDate = ''
		this.endDate = ''
		this.dimension = ''
		this.user_key = ''
		this.website_key = ''
	},
	setDimensionParam:function(dimension){
		switch(dimension) {
			case 'user' :
				this.user_key=$('#user_key').val()
				break
			case 'website' :
				this.website_key = $('#website_key').val()
				break
		}
	},
	setParam:function(){
		this.init()
		this.timeSize = $('input:radio[name="timeSize"]:checked').val();
		this.beginDate = $('#beginDate').val()
		this.endDate = $('#endDate').val()
		this.dimension = $('input:radio[name="latitude"]:checked').val();
		this.setDimensionParam(this.dimension)
	}
});
//第二版变更结束**********
$(function() {
	getList();
})

function getList() {
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 3,
			offset: '100px'
		});
		return;
	};
	
	//第二版变更开始**********
	param.setParam();
	
	//第二版变更结束**********
	//$("#table tbody tr").remove();
	$("#tbodyId").remove();
	$('#table').append($("<tbody id='tbodyId'></tbody>"));
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#table').DataTable({
		"aoColumnDefs" : [ {
			sDefaultContent : '',
			aTargets : [ '_all' ]
		} ],
		"bPaginate" : true, // 翻页功能
		"iDisplayLength" : 50,
		"bLengthChange" : false, // 改变每页显示数据数量
		"bFilter" : false, // 过滤功能
		"ordering": true, //排序功能
		"bSort" : true, // 排序功能
		"bInfo" : true,// 页脚信息
		"bAutoWidth" : true,// 自动宽度
		"iDisplayLength" : 50,
		// "sPaginationType": "full_numbers",//显示分页的样式
		// 还有simple_numbers等
//						"sScrollX" : "100%",
		"sScrollXInner" : "110%",
		"bScrollCollapse" : true,
		"oLanguage" : {
			"sLengthMenu" : "每页显示 _MENU_条",
			"sZeroRecords" : "没有找到符合条件的数据",
			"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
			"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
			"sInfoEmpty" : "没有记录",
			"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
			"sSearch" : "搜索：",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "前一页",
				"sNext" : "后一页",
				"sLast" : "尾页"
			}
		},
		"bProcessing" : true,
		"bServerSide" : true,
		"sAjaxSource" : "tcp_metr_perf_analy/query",
		"fnServerData" : function(sSource, aoData, fnCallback) {
			$.ajax({
				"type" : "post",
				"contentType" : "application/x-www-form-urlencoded",
				"url" : sSource,
				"dataType" : "json",
				"data" : {
					"aoData" : JSON.stringify(aoData),
					"param" : JSON.stringify(param)
				}, // 以json格式传递
				"success" : function(resp) {
					fnCallback(resp.pageDate);
					d = resp.allData;
				}
			});
		},
		columns : [
				{"data" : null,
					"render" : function(data, type, full, meta){
					return meta.row + 1 + meta.settings._iDisplayStart;
				}},
				{
					data : "useraccount"
				},
				{
					data : "website"
				},
				{
					data : "shake_delay"
				},
				{
					data : "first_packet_delay"
				},
				{
					data : "down_rate"
				},
				{
					data : "drop_packets_rate"
				}
			]
});
	switch(param.dimension) {
	case "user":
		var column = datatable.column(2);
	    column.visible(false);
		break;
	case "website":
		var column = datatable.column(1);
	    column.visible(false);
		break;
	}
	//第二版变更开始**********
//	var a=$("input[name='latitude']:checked");
//	if(a.attr('data-column')!=undefined){
//		if(a.val()=="area"){
//			if($("#tac").is(':checked')){
//				var column = datatable.column($("#tac").attr('data-column'));
//			    column.visible(true);
//			}
//			if($("#eNodeBID").is(':checked')){
//				var column = datatable.column($("#eNodeBID").attr('data-column'));
//			    column.visible(true);
//			}
//			if($("#village").is(':checked')){
//				var column = datatable.column($("#village").attr('data-column'));
//			    column.visible(true);
//			}
//		}
//		var column = datatable.column(a.attr('data-column'));
//	    column.visible(true);
//	    
//	    
//	}
	//第二版变更结束**********
}

function showPic() {

	var tab = $("#con");
	var pic = $("#pic");

	if (tab.is(":hidden")) {
		tab.show();
		pic.hide();
	} else {
		tab.hide();
		pic.show();

		initAttachChar();
	}
}


function resetQueryForm() {
	$('#queryForm')[0].reset();

	$("#eNodeBID").attr("disabled",true);
	$("#tac").attr("disabled",true);
	$("#village").attr("disabled",true);
	
	$("#eNodeBID_value").attr("readonly",true);
	$("#tac_value").attr("readonly",true);
	$("#village_value").attr("readonly",true);
}

function exportExcel() {
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/attachment/exportBigDataExcel?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			if (data.flag) {
				var url = "../../businessAnalysis/attachment/downFiles?path="
						+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		}
	});
}

function forwordInterfaceFailure(dateType,failureTypeInfo,jspType,mme_name,city_code,tai,
		enodeb_id,enodeb_name,cell_name,group_id,msisdn,user_terminal,terminal_brand,apn_name,paramDate) {
	var url = "/kpi/interfaceFailureLink/index?dateType=" + dateType +"&failureTypeInfo="+failureTypeInfo + "&jspType="+jspType +"&mme_name="+mme_name
	+ "&paramDate=" + paramDate + "&interfaceType=2&signalType=1&type=" + param.dimension +"&tac_status="+param.tac_status
	+ "&eNodeBID_status=" + param.eNodeBID_status +"&village_status="+param.village_status
	+ "&city_code=" +city_code+"&tai="+tai+"&enodeb_id="+enodeb_id+"&enodeb_name="+enodeb_name  
	+ "&cell_name=" +cell_name+"&group_id="+group_id+"&msisdn="+msisdn+"&user_terminal="+user_terminal+"&terminal_brand="+terminal_brand+"&apn_name="+apn_name;   
	linkTab("接口故障", url);
}

// 下拉树 begin
function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getSelectedNodes(),
	v = "";
	nodes.sort(function compare(a,b){return a.id-b.id;});
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0 ) v = v.substring(0, v.length-1);
	var cityObj = $("#citySel");
	cityObj.attr("value", v);
	$("#area").val(treeNode.id);
	
	hideMenu();
}
function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({left:cityOffset.left-26, top:cityOffset.top-106}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}
//下拉树 end

function checkEndTime(){  
    var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    if(end<start){  
        return false;  
    }  
    return true;  
}